package com.qd.panda.carpark;

import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.qd.common.panda.domain.entity.carpark.CarParkDoorpostDTO;
import com.qd.common.panda.domain.entity.carpark.CarParkDoorpostLoginDTO;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.park.CarParkDoorpostLoginService;
import com.qd.panda.service.park.CarParkDoorpostService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Optional;

/**
 * APP_门岗登录信息
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/carParkDoorpostLogin/")
@Slf4j
public class AppCarParkDoorpostLoginController {
    private final CarParkDoorpostLoginService carParkDoorpostLoginService;
    private final CarParkDoorpostService carParkDoorpostService;

    /**
     * 门岗列表
     *
     * @param parkId 停车场ID
     * @return 停车场对应的门岗
     */
    @GetMapping(value = "/doorpostList")
    public List<CarParkDoorpostDTO> doorpostList(@Validated @NotBlank(message = "停车场ID不能为空") String parkId) {
        CarParkDoorpostDTO query = new CarParkDoorpostDTO();
        query.setParkId(parkId);
        final List<CarParkDoorpostDTO> list = carParkDoorpostService.getListDto(query);
        query.freeData();
        return list;
    }

    /**
     * 删除门岗登录信息
     *
     * @return 页面对象
     */
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            carParkDoorpostLoginService.removeById(ids.get(0));
        } else {
            carParkDoorpostLoginService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取门岗登录信息
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public CarParkDoorpostLoginDTO getById(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return carParkDoorpostLoginService.getByIdDto(id);
    }

    /**
     * 门岗登录信息列表
     *
     * @param parkId 停车场ID
     * @param userId 用户ID
     * @return 列表信息
     */
    @GetMapping(value = "/list")
    public List<CarParkDoorpostLoginDTO> list(@Validated @NotBlank(message = "停车场ID不能为空") String parkId, String userId) {
        userId = Optional.ofNullable(userId).orElse(SecurityUtils.getLoginUser().getUserId());
        final CarParkDoorpostLoginDTO query = new CarParkDoorpostLoginDTO();
        query.setParkId(parkId);
        query.setOperator(userId);
        final List<CarParkDoorpostLoginDTO> list = carParkDoorpostLoginService.getListDto(query);
        query.freeData();
        return list;
    }

    /**
     * 登录
     */
    @PostMapping(value = "/login")
    public ApiResultWrapper<String> login(@Validated @RequestBody DelDTO<String> delDTO) {
        carParkDoorpostLoginService.login(SecurityUtils.getLoginUser().getUserId(), delDTO.getIds());
        return ApiResult.success("登录成功");
    }

    /**
     * 退出
     */
    @PostMapping(value = "/logout")
    public ApiResultWrapper<String> logout(@Validated @RequestBody DelDTO<String> delDTO) {
        carParkDoorpostLoginService.logout(SecurityUtils.getLoginUser().getUserId(), delDTO.getIds());
        return ApiResult.success("退出成功");
    }
}